from manim import *
import math
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [5, 20, 0],
            [8,7,0],
            [25,4,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [7, 20, 0],
            [9,6,0],
            [25,2,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [3, 20, 0],
            [7,8,0],
            [25,6,0]
        ]
        self.set_points_smoothly(points)
class CustomCurve3(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [0, 0, 0],
            [20,20,0]
        ]
        self.set_points_smoothly(points)



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
         #镜头跟进效果 
        self.camera.frame.save_state()
         
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)

        # 创建坐标系
        axes = Axes(
            x_range=[-10, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-10, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=60,  # 控制 x 轴长度
            y_length=40,  # 控制 y 轴长度
        )
        axes.move_to([0, 0, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        title = Tex(
                    r"$\dfrac{1}{x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,24,0])

       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve = CustomCurve(color=PURE_RED, stroke_width=20)
        
        self.play(Create(axes))
        self.play( Write(title), Create(curve))

        text = Tex(
                    r"$\int _{0}^{+\infty }\dfrac{1}{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,17,0])
        self.play(Write(text))
        text1 = Tex(
                    r"\text{发散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,RIGHT,buff=1)
        self.play(Write(text1))
        
        text2 = Tex(
                    r"$\int _{0}^{1}\dfrac{1}{x}dx+\int _{1}^{+\infty }\dfrac{1}{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,17,0])
        self.play(
            ReplacementTransform(text,text2),
            ReplacementTransform(text1,text2)
        )
        self.wait(0.5)
        text3 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-26,10,0])
        text4 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-14,10,0])
        self.play(Write(text3),Write(text4))
        #画竖直线
        dot1=Dot([7,20,0])
        dot2=Dot([7,0,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        self.wait(0.3)
        #半透明矩阵扩展
        rectangle=Rectangle(color=YELLOW,fill_opacity=0.5,width=5,height=1
                            ,stroke_color=YELLOW,stroke_width=0).move_to([2.5,0,0])
        self.play(
            rectangle.animate.shift(10*UP).stretch(20.0,dim=1)
        )
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=4
                            ,stroke_color=YELLOW,stroke_width=0).move_to([7,2,0])
        self.play(
            rectangle1.animate.shift(12*RIGHT).stretch(21.0,dim=0)
        )
        self.play(FadeOut(rectangle),FadeOut(rectangle1),FadeOut(line))
        curve1 = CustomCurve1(color=BLUE, stroke_width=20)
        # 添加标题
        title1 = Tex(
                    r"$\dfrac{1}{x^{2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([8,24,0])

        self.play(Create(curve1),Write(title1))



        text200 = Tex(
                    r"$\int _{0}^{1}\dfrac{1}{x^{2}}dx+\int _{1}^{+\infty }\dfrac{1}{x^{2}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,17,0])
        self.play(
           FadeOut(text3),FadeOut(text4),FadeOut(text2),Write(text200)
        )
        self.wait(0.5)
        text300 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-26,10,0])
        text400 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-14,10,0])
        self.play(Write(text300))
        self.play(Write(text400))

















        curve2 = CustomCurve2(color=YELLOW, stroke_width=20)
        # 添加标题
        title2 = Tex(
                    r"$\dfrac{1}{\sqrt{x}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([2,24,0])

        self.play(Create(curve2),Write(title2))




        text2000 = Tex(
                    r"$\int ^{1}_{0}\dfrac{1}{\sqrt{x}}dx+\int ^{+\infty }_{1}\dfrac{1}{\sqrt{x}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,17,0])
        self.play(
            
            FadeOut(text300),FadeOut(text400),FadeOut(text200),Write(text2000)

        )
        self.wait(0.5)
        text3000 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-26,10,0])
        text4000 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-14,10,0])
        self.play(Write(text3000))
        self.play(Write(text4000))









        #箭头
        arrow = Arrow(start=[0,27.5,0],end=[15,27.5,0],color=RED
                       , stroke_width=20, tip_length=1)
        text5 = Tex(
                    r"$\dfrac{1}{x^{p}}$",color=ORANGE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,25,0])
        self.play(GrowArrow(arrow),Write(text5))
        text6 = Tex(
                    r"\text{P依次增加(p>0)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,25,0])
        self.play(Write(text6))
        self.wait()



        curve3 = CustomCurve3(color=PINK, stroke_width=20)
        # 添加标题
        title3 = Tex(
                    r"$x$\text{  }\text{(p<0)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,20,0])

        self.play(Create(curve3),Write(title3))



        text20000 = Tex(
                    r"$\int _{0}^{1}xdx+\int _{1}^{+\infty }xdx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,17,0])
        self.play(
            
            FadeOut(text3000),FadeOut(text4000),FadeOut(text2000),Write(text20000)

        )
        self.wait(0.5)

         #画竖直线
        dot1=Dot([7.5,20,0])
        dot2=Dot([7.5,0,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        self.wait(0.3)

        #画横线
        dot3=Dot([0,8,0])
        dot4=Dot([7,8,0])
        line1=Line(dot3,dot4).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))
        self.wait(0.3)

        #画椭圆半透明
        # 创建一个椭圆
        ellipse = Ellipse(
            width=4,  # 长轴长度
            height=2,  # 短轴长度
            color=BLUE,  # 边界颜色
            fill_color=BLUE,  # 填充颜色
            fill_opacity=1,  # 填充透明度
        )
        
        # 设置椭圆的长轴和短轴方向
        # 旋转椭圆，例如，旋转 30 度
        rotation_angle = 30 * DEGREES
        ellipse.rotate(rotation_angle)
        
        # 将椭圆移动到场景中心
        ellipse.move_to([2.5,5,0])

        # 添加椭圆到场景中
        self.add(ellipse)

        # 动画显示椭圆
        self.play(Create(ellipse))
        self.wait(0.5)





        text30000 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-26,10,0])
        self.play(Write(text30000))
        self.play(ellipse.animate().scale(5).move_to([20,10,0])) 
        text40000 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-14,10,0])
        
        self.play(Write(text40000))



        title4 = Tex(
                    r"$x^{2},x^{3}\ldots $}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-10,0])

        self.play(Write(title4))
        title5 = Tex(
                    r"\text{总结：}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-5,0])

        self.play(Write(title5),FadeOut(ellipse))
        title6 = Tex(
                    r"$\int _{0}^{1}\dfrac{1}{x^{p}}dx+\int ^{+\infty }_{1}\dfrac{1}{x^{p}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-5,0])

        self.play(Write(title6))
        title7 = Tex(
                    r"$p<0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7,-10,0])

        self.play(Write(title7))
        text3 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,-10,0])
        text4 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([31,-10,0])
        self.play(Write(text3),Write(text4))
        title8 = Tex(
                    r"$0<p<1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7,-15,0])

        self.play(Write(title8))
        text3 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,-15,0])
        text4 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([31,-15,0])
        self.play(Write(text3),Write(text4))
        title9 = Tex(
                    r"$p=1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7,-20,0])

        self.play(Write(title9))
        text3 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,-20,0])
        text4 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([31,-20,0])
        self.play(Write(text3),Write(text4))
        title10 = Tex(
                    r"$p>1$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7,-25,0])

        self.play(Write(title10))
        text3 = Tex(
                    r"\text{散}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,-25,0])
        text4 = Tex(
                    r"\text{收}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([31,-25,0])
        self.play(Write(text3),Write(text4))

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=33,height=7.2
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([17, -12.5, 0])
        self.play(Create(rectangle))



        

        

        self.wait()